import collections
class Solution(object):
    def calculate(self, s):
        """
        :type s: str
        :rtype: int
        """
        s = list(s)
        s = [item for item in s if item != ' ']
        s = ''.join(s)
        left = 0
        right = 0
        n = len(s)
        state = 0
        stack = collections.deque()
        while right < n:
            if ord('0') <= ord(s[right]) <= ord('9'):
                right += 1
            else:
                stack.append(int(s[left: right]))
                if state == 1:
                    num2 = stack.pop()
                    op = stack.pop()
                    num1 = stack.pop()
                    if op == '*':
                        stack.append(num1 * num2)
                    else:
                        stack.append(int(num1 / num2))
                    state = 0
                stack.append(s[right])
                if s[right] == '*' or s[right] == '/':
                    state = 1
                right += 1
                left = right
        stack.append(int(s[left:]))
        if state == 1:
            num2 = stack.pop()
            op = stack.pop()
            num1 = stack.pop()
            if op == '*':
                stack.append(num1 * num2)
            else:
                stack.append(int(num1 / num2))
        while len(stack) >= 3:
            num1 = stack.popleft()
            op = stack.popleft()
            num2 = stack.popleft()
            if op == '+':
                stack.appendleft(num1 + num2)
            else:
                stack.appendleft(num1 - num2)
        return stack[0]


data = Solution()
s = "35"
print(data.calculate(s))
s = "3 /2"
print(data.calculate(s))
s = " 3+5 / 2 "
print(data.calculate(s))

